package com.activemq.service.impl;

import com.activemq.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;

/**
 * 消息生产者
 */
@Service
public class ProducerServiceImpl implements ProducerService {

    //========================== 点对点模式 ==============================
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue queue;

    /**
     * 指定消息队列，发送消息
     * @param destination
     * @param message
     */
    @Override
    public void sendMessage(Destination destination, String message) {
        jmsMessagingTemplate.convertAndSend(destination,message);
    }

    /**
     * 使用默认消息队列，发送消息
     * @param message
     */
    @Override
    public void sendMessage(String message) {
        jmsMessagingTemplate.convertAndSend(queue,message);
    }

    //========================== 发布订阅模式 ==============================
    @Autowired
    private Topic topic;
    /**
     * 发布订阅：消息发布者
     * @param message
     */
    @Override
    public void publish(String message) {
        jmsMessagingTemplate.convertAndSend(topic,message);
    }
}
